//
//  SetTimeTableViewCell.m
//  HaoHint
//
//  Created by winter on 16/5/25.
//  Copyright © 2016年 wd. All rights reserved.
//

#import "SetTimeTableViewCell.h"

@interface SetTimeTableViewCell ()
@property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;
@property (strong, nonatomic) UIDatePicker *datePickerIOS6;
@end

@implementation SetTimeTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (!self) {
        return nil;
    }
    [self setUpDatePicker];
    return self;
}

- (void)setUpDatePicker
{
    self.datePickerIOS6 = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, -0.5, CGRectGetWidth(self.frame), 216)];
    [self addSubview:self.datePickerIOS6];
    
    self.datePickerIOS6.datePickerMode = UIDatePickerModeCountDownTimer;
    
    [self.datePickerIOS6 addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    // 可以解决datePicker在第一次选择时，事件不响应bug
    // http://stackoverflow.com/questions/29239681/uidatepickers-value-changed-event-isnt-triggered-when-value-is-changed-for-the?answertab=active#tab-top
    dispatch_async(dispatch_get_main_queue(), ^{
        self.datePicker.countDownDuration = 0;
    });
    
}

- (IBAction)dateChanged:(UIDatePicker *)sender
{
    if (self.setTimeBlock) {
        self.setTimeBlock(sender.countDownDuration);
    }
}

@end
